<?php
require_once('init.php');

require_once('model/class.cates.php');
require_once('model/class.mlog.php');
require_once('model/class.user.php');
require_once('model/class.comment.php');
require_once('model/class.attachment.php');

$qiCate = new qiCate($DB);
$qiMlog = new qiMlog($DB);
$qiUser = new qiUser($DB);
$qiComment = new qiComment($DB);
$qiAttachment = new qiAttachment($DB);

//获得话题id
$cid=isset($_GET["id"]) ? intval($_GET['id']) : '';


if($action == '')
{
$pageurl = '';

if($cid==""){
redirect('index.php', 3, '不好意思，你查看的话题不存在!3秒钟后跳转到首页！'); 
}

$cateDate = $qiCate->getOneCate($cid);
$title = $cateDate['cname'];
$page = isset($_GET['page']) ? abs(intval($_GET['page'])) : 1;
$pageurl .= './topic.php?id='.$cid.'&page';

$mlogs = $qiMlog->getCateMlog($page,8,$cid);

$virtue = "cid";
$setvirtue = $cid;
$lognum = $qiMlog->getLogNum($virtue, $setvirtue);

foreach($mlogs as $key=>$val)
{
$val['cname'] = $qiCate->getCateName($val['cid']);
$val['truename'] = $qiUser->getUserName($val['uid']);
$val['comms'] = $qiComment->getComments($val['id']);
$val['photo'] = $qiAttachment->getMlogPic($val['aid']);
$mlogs[$key] = $val;
}

$page_url = pagination($lognum, 8, $page, $pageurl);

// 输出模板
$xingTemplate->display('topic');
}

//发布微博
if($action == 'addlog')
{
$logcontent = t($_POST['logcontent']);
$aid = $_POST['uploadvalue'];
$ctime = time();
if(empty($uid))
{
redirect('log.php?action=in', 2, '请登录后再发表！');
}
elseif(empty($logcontent))
{
redirect('topic.php?id='.$cid.'', 2, '话题微博内容不能为空！3秒钟跳转到上一页！');
}else
{
$qiMlog->addMlog($cid, $uid, $logcontent,$aid,$ctime);
redirect('topic.php?id='.$cid.'', 2, '发布成功，3秒钟跳转到话题页！');
}

}


//所有话题
if($action == "all")
{
$title = "全部话题";
$pageurl = '';
$page = isset($_GET['page']) ? abs(intval($_GET['page'])) : 1;
$pageurl .= 'topic.php?action=all&page';

require_once('model/class.cates.php');
$qiCate = new qiCate($DB);
$cates = $qiCate->getCate($page,20);

$virtue = "id";
$setvirtue = "id";
$catenum = $qiCate->getCateNum($virtue, $setvirtue);

$page_url = pagination($catenum, 20, $page, $pageurl);

$xingTemplate->display('topic_all');
}

?>